﻿namespace Sitecore.ChangesTracker.Archiving
{
  using System.Xml;
  using Configuration;
  using Data;
  using Data.Archiving;
  using Diagnostics;
  using Sitecore.Configuration;
  using Xml;
  using System.Linq;
  using System.Collections.Generic;

  public class ChangeTrackerArchiveProvider : SqlArchiveProvider
  {
    public bool UseExternalStorage { get; set; }

    public override Archive GetArchive(string archiveName, Database database)
    {
      Assert.ArgumentNotNullOrEmpty(archiveName, "archiveName");
      Assert.ArgumentNotNull(database, "database");
      XmlNode configNode = Factory.GetConfigNode(string.Format("databases/database[@id='{0}']/archives/archive[@name='{1}']", database.Name, archiveName));

      if (configNode == null)
      {
        return null;
      }

      return  this.GetArchive(configNode, database);
    }

    protected override Archive GetArchive(XmlNode configNode, Database database)
    {
      string attribute = XmlUtil.GetAttribute("name", configNode);
      if (string.IsNullOrEmpty(attribute))
      {
        return null;
      }

      string connection = string.Empty;
      XmlNode node=configNode.SelectNodes("param[@name='ConnectionstringName']")[0];
      if(node!=null)
      {
        connection = node.InnerText;
      }
      return new ChangeTrackerArchive(attribute, database,connection);
    }
  }
}

